package com.food.eat.impl;

import com.food.eat.mapper.StuMapper;
import com.food.eat.pojo.Stu;
import com.food.eat.service.FoodEatService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author:RenPu
 * @Date: 2020/7/18 23:00
 * @Version: 1.0
 * @description:
 */

@Service
public class FoodEatServiceImpl implements FoodEatService {


    @Resource
    private StuMapper stuMapper;

    /**
     * REQUIRED：如果没有事务，创建一个新事务，如果有事务加入当前事务
     *
     *
     */


    /**
     * stu的保存方法
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveStu() {

        Stu stu = new Stu();
        stu.setAge(18);
        stu.setName("lining");
        stuMapper.insert(stu);

    }


    /**
     * stu的删除方法
     *
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteOneStu(int id) {

        stuMapper.deleteByPrimaryKey(id);

    }


    /**
     * stu根据唯一id获取对象的相信信息
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Stu getStuInfo(int id) {
        return stuMapper.selectByPrimaryKey(id);
    }


    /**
     * stu根据唯一id值进行修该，对象的信息
     *
     * @param id
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void updateOneStu(int id) {
        Stu stu = new Stu();
        stu.setAge(18);
        stu.setName("lucny");
        stu.setId(id);
        stuMapper.updateByPrimaryKey(stu);


    }

    @Override
    public void saveParent() {

        Stu stu = new Stu();
        stu.setAge(18);
        stu.setName("lucnyParent");
        stuMapper.insert(stu);

    }


    //@Transactional(propagation = Propagation.REQUIRED)
    //@Transactional(propagation = Propagation.SUPPORTS)
    //@Transactional(propagation = Propagation.MANDATORY)
    //@Transactional(propagation = Propagation.REQUIRES_NEW)
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public void saveChildren() {

        saveChildren1();

        /**
         * 模仿异常
         */
        //int a=1/0;


        saveChildren2();

    }


    public void saveChildren1() {

        Stu stu = new Stu();
        stu.setAge(18);
        stu.setName("lucnyChildren1");
        stuMapper.insert(stu);

    }


    public void saveChildren2() {
        Stu stu = new Stu();
        stu.setAge(18);
        stu.setName("lucnyChildren2");
        stuMapper.insert(stu);

    }


}
